package com.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @author cyt
 * @date 2020/9/19
 */

@Configuration // 声明为spring配置类
@ComponentScan("com") // 扫描包
@Import(DataSourceConfig.class) // 导入其他配置类
@EnableTransactionManagement
@EnableAspectJAutoProxy
public class SpringConfig {

    /**
     * 注入jdbcTemplate
     * @param dataSource
     * @return
     */
    @Bean
    public JdbcTemplate getJdbcTemplate(@Autowired DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }


    /**
     * 注入事务管理的类
     * @param dataSource
     * @return
     */
    @Bean("transactionManager")
    public PlatformTransactionManager getPlatformTransactionManager(@Autowired DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }


}
